local Msg = require("core.Msg")
local RebateExcel = require("excel.OperateActivity").chongzhiRebate
local Grid = require("bag.Grid")
local ActDefine = require("activity.Define")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local OperateActFL = require("activity.OperateActFL")
local HtmlUtil = require("common.HtmlUtil")
local ChongzhiDraw = require("activity.operate.ChongzhiDraw")
local Log = require("common.Log")

function open(human, id, type)
  local sendMsg = Msg.gc.GC_OPEN_YUNYING_ACTIVITY
  local activityNum
  if type == 2 then
    activityNum = OperateActFL.onGetTomorrowActRuleID(id)
  else
    activityNum = OperateActFL.onGetCurActRuleID(id)    
  end
  local activity = RebateExcel[activityNum]
  if not activity then
    return
  end
  sendMsg.info[0] = #activity.condition
  local cache = {}
  for i=1, #activity.condition do
    sendMsg.info[i].reward[0] = #activity.reward[i]
    for j=1, #activity.reward[i] do
      Grid.create(cache, activity.reward[i][j][1], activity.reward[i][j][2])
      Grid.makeItem(cache, sendMsg.info[i].reward[j], 0, human)
    end
    sendMsg.info[i].args[0] = 1
    sendMsg.info[i].args[1] = activity.condition[i]
    sendMsg.info[i].canGet = human.db.YYChongzhiRebate[i - 1]
    sendMsg.info[i].name = ""
  end
  sendMsg.myInfo[0] = 1
  sendMsg.myInfo[1] = human.db.dailyChongzhi
  sendMsg.activityId = id 
  sendMsg.type = type or 1 
  Msg.send(sendMsg, human.fd)
end
--领取奖励
function getReward(human, id, index)  
  if human.db.YYChongzhiRebate[index - 1] ~= ActDefine.REWARD_CAN_GET then
    return
  end
  local activityNum = OperateActFL.onGetCurActRuleID(id)
  local activity = RebateExcel[activityNum]
  if not activity then
    return
  end
  if not BagLogic.checkCanAddItemList(human, activity.reward[index], #activity.reward[index]) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YUNYING_BAG_NOT_ENOUGH)
  end
  
  for i=1, #activity.reward[index] do
    BagLogic.addItem(human, activity.reward[index][i][1], 1, activity.reward[index][i][2], "yunyingAcvity")
  end
  local str = string.format(Lang.YUNYING_CHONGZHI_REBATE_BRO, HtmlUtil.fontName(human.name, human._id), human._id)
  Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
  human.db.YYChongzhiRebate[index - 1] = ActDefine.REWARD_ALREADY_GET
  open(human, id)
  Log.write(Log.LOGID_OSS_YUNYING_CHONGZHI_REBATE, human._id, human.account, human.name,human.db.lv, activity.condition[index])
end

--检查是否可领取奖励
function checkCanGet(human)
  if OperateActFL.onCheckActIsRunning(ActDefine.OPERATEACT_TYPE_CHONGZHIREBATE) then
    local activityNum = OperateActFL.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_CHONGZHIREBATE)
    local activity = RebateExcel[activityNum]
    if not activity then
      return
    end
    local change = false
    for i=1, #activity.reward do
      if human.db.dailyChongzhi >= activity.condition[i] and human.db.YYChongzhiRebate[i - 1] == ActDefine.REWARD_NOT_CAN_GET then
        change = true
        human.db.YYChongzhiRebate[i - 1] = ActDefine.REWARD_CAN_GET
      end
    end
    if change then
      open(human, ActDefine.OPERATEACT_TYPE_CHONGZHIREBATE)
    end
  end
  if OperateActFL.onCheckActIsRunning(ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW) then
    ChongzhiDraw.open(human, ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW)
  end
end
